package adapter;

import java.util.ArrayList;
import lfm.mpr.R;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import objects.User;

public class AdapterListUserSearch extends ArrayAdapter<User>{

	private ArrayList<User> list_user;
	private Context context;

	public AdapterListUserSearch(Context context, ArrayList<User> list) {
		super(context, R.layout.list_view_user_search, list);
		this.context = context;
        this.list_user = list;
       
        
	}//end
	
	static class ViewHolder {
        protected TextView fullname, mail, user;
        protected ImageView img;
    }
	  
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
        final ViewHolder viewHolder;
        
        if (convertView == null) {
        	
        	viewHolder = new ViewHolder();
        	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	
	        convertView  = inflater.inflate(R.layout.list_view_user_search, null);
	            
	        viewHolder.fullname = (TextView) convertView.findViewById(R.id.text_user_fullname);
	        //viewHolder.mail = (TextView) convertView.findViewById(R.id.textView_user_mail);
	        //viewHolder.user = (TextView) convertView.findViewById(R.id.text_lst_user);
	        viewHolder.img = (ImageView)  convertView.findViewById(R.id.foto_jugador_team);
	       
            convertView.setTag(viewHolder);

        } else {
        	viewHolder = (ViewHolder) convertView.getTag();
        }
        
       
	        viewHolder.fullname.setText(this.list_user.get(position).getUser());
	        //viewHolder.mail.setText(" " + String.valueOf(this.list_user.get(position).getEmail()));
	        //viewHolder.user.setText(this.list_user.get(position).getUser());
	        
	        if(this.list_user.get(position).getBitmap_image_Server() == null){
		        String uri = "";
		        uri = "drawable/" + "perfil";
		        int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
		        Drawable img1;
		        if(imageResource == 0) img1 = context.getResources().getDrawable(R.drawable.ic_launcher);
		        	else img1 = context.getResources().getDrawable(imageResource);
		        viewHolder.img.setImageDrawable(img1);
	        }
	        else {
	        	Drawable d =new BitmapDrawable(this.list_user.get(position).getBitmap_image_Server());
	        	viewHolder.img.setImageDrawable(d);
	        }
        return convertView;
        }
    }//end_class
